<?php

class Welcome extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('model_user');			
		$this->load->library('form_validation');
		$this->load->library('session');
	}
	
	function index(){
		if( $this->session->userdata('userid') == null) {
			$this->login();
		}
		else
		{
			$this->home();			
		}  
	}//end index
	
	function login(){
		$this->session->sess_destroy(); 		
		$valid = false; 
		
		$this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
						
		if($this->form_validation->run() == TRUE)
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
		
			$users = $this->model_user->get_user();
			$users_obj	= $users->result(); 
		
			foreach ($users_obj as $row){	
				if($username == $row->USERNAME && md5($password) == $row->PASSWORD){
					$valid = true;
					$newdata = array(
						'userid' => $row->ID_USER,
						'username' => $username ,
						'roleid' => $row->ID_ROLE,
						'password' => $password,
						'nama'	   => $row->NAMA
						);
					$this->session->set_userdata($newdata);
					break;
				}			
			}
			
			if($valid){ 
				$this->home();
			}
			else{
				$data['cek_form'] = 1;
				$this->load->view('welcome',$data);
			}
		
		}
		else 
		{
			$this->load->view('welcome');
		} // end foreach
			 
	}//end login
	
	function home()
	{
		//if($this->session->userdata('userid') == null) redirect('welcome');
		$data['content'] = $this->load->view('home',null,true);
		$this->load->view('main',$data);
                
	}//end home
        
	function logout()
	{		
		$this->session->sess_destroy();
		$this->login();	
	}
	
}//end class

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
